<?php

namespace App\Http\Controllers\Laifei\Shop;

use App\Http\Controllers\Controller;
use App\Models\Common\Common;
use Illuminate\Support\Facades\DB;

class ShopVerifyController extends Controller
{

    public function index()
    {
        $all = $this->all;

        if (!isset($all['shop_id'])) {
            return $this->setReturnJson(-1, '缺少参数:shop_id');
        }
        if (!isset($all['verified'])) {
            return $this->setReturnJson(-1, '缺少参数:verified');
        }
        if ($all['verified'] === 2 && empty($all['verify_remark'])) {
            return $this->setReturnJson(-1, '拒回时请输入拒回原因');
        }

        DB::table('shop')->where('shop_id', '=', $all['shop_id'])->update([
            'verified' => $all['verified'],
            'verified_by' => $this->userInfo['id'],
            'verified_at' => date('Y-m-d H:i:s'),
            'verify_remark' => $all['verify_remark'],
        ]);

        // 发送推送
        $shop_message = DB::table('shop')->where('shop_id', '=', $all['shop_id'])->first();
        $shop_message = json_decode(json_encode($shop_message), true);
        $openid = DB::table('lottery_users')->where('we_id', '=', $shop_message['we_id'])->value('openid');
        if ($all['verified'] === 1) { // 通过
            if ($all['verify_remark']) { // 有备注
                $data = [
                    'first' => '来飞网对您的店铺信息进行了审核，您虽通过了审核，但为提高您后续的使用体验，建议您对店铺信息进行一些修改。',
                    'keyword1' => '店铺【' . $shop_message['shop_name'] . '】',
                    'keyword2' => date('Y-m-d H:i:s'),
                    'remark' => '点击打开店铺编辑页面，按照提示修改并提交店铺【' . $shop_message['shop_name'] . '】的信息，我们将尽快为您再次审核。',
                ];

            } else {
                $data = [
                    'first' => '您提交的店铺信息信息已通过审核',
                    'keyword1' => '店铺【' . $shop_message['shop_name'] . '】',
                    'keyword2' => date('Y-m-d H:i:s'),
                    'remark' => '感谢您选择来飞网为您服务。',
                ];
            }
            Common::sendLaifeiMessage($openid, 'certified', $data, '/shop');
        } else { // 拒回
            $data = [
                'first' => '来飞网对您的店铺信息进行了审核，您需要对店铺信息进行一些修改。',
                'keyword1' => '已拒回',
                'keyword2' => [
                    'value' => $all['verify_remark'],
                    'color' => '#F00'
                ],
                'remark' => '点击打开当前页面，按照失败原因修改提交店铺【' . $shop_message['shop_name'] . '】的信息，我们将尽快为您再次审核。',
            ];
            Common::sendLaifeiMessage($openid, 'certify_fail', $data, '/shop');
        }


        return $this->setReturnJson(0, 'success');

    }

}
